'''
Created on Nov 22, 2010

@author: Adam
'''
from util.plotting import plotList
from sim.SimulateAcceptanceRates import getStats, simOverlapPrep



# Acceptance probablity simulation by doing 100000 complete verifications for each schedule.
# Exception: 12-qubit verification was simulated for only 50000 verifications.

#Steane Random
dataSteaneRandom = [
['p', r'$\Pr[X_1]$', r'$\Pr[X_2]$', r'$\Pr[Z \vert X_1, X_2]$', r'$\Pr[X_1, X_2, Z]$'],				
[0.0005, 0.92841302841302842, 0.92812359278118395, 0.93304471150257517, 0.80398786623456675],
[0.0006, 0.91401327537989829, 0.91430593707342556, 0.92159103476241389, 0.77016235138770317],
[0.0007, 0.9029618210443453, 0.90142436149312377, 0.90811674748905724, 0.73916324577958847],
[0.0008, 0.88565584168463785, 0.88841700069987917, 0.89520706139330031, 0.70437729980645003],
[0.0009, 0.87621922898281468, 0.87510823849579411, 0.88347027122537325, 0.67743322379299753],
[0.001, 0.86200437558066356, 0.86201729279367034, 0.86918730986527593, 0.64586065033841089],
[0.0011, 0.85008350669518595, 0.84769905040175308, 0.86169754416199917, 0.62095215974812701],
[0.0012, 0.83353731448266155, 0.83813565315793193, 0.84385337203807465, 0.58953059939363572],
[0.0013, 0.82363716497259176, 0.8245002068109748, 0.83610641962508991, 0.56779068314669223],
[0.0014, 0.81168431750106973, 0.81333744238396399, 0.82370965881945934, 0.54379107989941966],
[0.0015, 0.79895635919935615, 0.80127579248844627, 0.81234768480909825, 0.52005230697087546],
[0.0016, 0.79067874224076529, 0.78612892210797891, 0.80437580437580436, 0.49998023437212463],
[0.0017, 0.7759435990026653, 0.778909341980347, 0.79145231499802138, 0.47834564156155773],
[0.0018, 0.76563450868086624, 0.76600926412301229, 0.77925316376784493, 0.45701883188533637],
[0.0019, 0.75380391791912893, 0.75569394395269918, 0.77160493827160492, 0.43954093803958583],
[0.002, 0.7452367434994891, 0.74333767484002489, 0.76181189340727984, 0.42201525765869474],
[0.0021, 0.72792998477929982, 0.73266947529682114, 0.74677021880367411, 0.3982765140774196],
[0.0022, 0.71988240557295324, 0.7193613624268228, 0.7398200757575758, 0.38311996039007629],
[0.0023, 0.71001517920758339, 0.71119753418423282, 0.72716695753344973, 0.36719098653709242],
[0.0024, 0.69771701380200779, 0.70159318042290231, 0.71724691942448104, 0.35110204899407599],
[0.0025, 0.69107198625858834, 0.69145663870633844, 0.70611495551475778, 0.33741442785092246]
]

#Steane Random: No rests
dataSteaneRandom_noRests = [
['p', r'$\Pr[X_1]$', r'$\Pr[X_2]$', r'$\Pr[Z \vert X_1, X_2]$', r'$\Pr[X_1, X_2, Z]$'],				
[0.0005, 0.93141374196737514, 0.93254971277065846, 0.94770560473094634, 0.8231672487559657],
[0.0006, 0.91808215877605104, 0.91871458832038855, 0.93725982716928791, 0.79053693042179818],
[0.0007, 0.9061365774969754, 0.90422214024615177, 0.92719652857619705, 0.75969732175540372],
[0.0008, 0.89068528084744658, 0.89307959611854182, 0.91768376617417635, 0.72997416801685555],
[0.0009, 0.87754484340650585, 0.88181527133023851, 0.90765516365022603, 0.70237301377181516],
[0.001, 0.86950876095118901, 0.8665263486124104, 0.89962036020799219, 0.67782098608605312],
[0.0011, 0.85528601468929999, 0.85546851185758699, 0.890805109658109, 0.65177560102519361],
[0.0012, 0.84168591908062318, 0.84614697474988088, 0.87975507618678961, 0.62655276253619518],
[0.0013, 0.83079349918233258, 0.83229912720735355, 0.87097392304074417, 0.60225121000836013],
[0.0014, 0.81917159095716929, 0.82058345694709334, 0.86080743737625898, 0.57863360242789397],
[0.0015, 0.80728987639383487, 0.80894712187594719, 0.8515711487694797, 0.5561226450041572],
[0.0016, 0.79639489813709585, 0.79840802441746328, 0.84203435500168411, 0.53540592562966116],
[0.0017, 0.78866026760507901, 0.78975950349107837, 0.83247311111851086, 0.51850749339593105],
[0.0018, 0.77612932081017183, 0.7736774390787613, 0.8232349841938883, 0.49433099423599847],
[0.0019, 0.76538744559049132, 0.7640818765874795, 0.81475687654803808, 0.47648503759555466],
[0.002, 0.75437380700538592, 0.75250742829422113, 0.80582774626095921, 0.45744576254040747],
[0.0021, 0.74400867565216355, 0.74309655303751665, 0.80087134802665305, 0.44277796827518778],
[0.0022, 0.73140280305577132, 0.73291514201080332, 0.79248094083337295, 0.42481431321122803],
[0.0023, 0.72357456264668196, 0.72154574952797657, 0.78345346286430584, 0.40903490296480571],
[0.0024, 0.71194482273865478, 0.71224207264816397, 0.77131926446994936, 0.39111830198576852],
[0.0025, 0.70241512528867112, 0.70095521166499752, 0.76816715317253037, 0.37821596468229851],
]

#Overlap
dataOverlap = [
['p', r'$\Pr[X_1]$', r'$\Pr[X_2]$', r'$\Pr[Z \vert X_1, X_2]$', r'$\Pr[X_1, X_2, Z]$'],
[0.0005, 0.92528853727867388, 0.92670085235555399, 0.92532617747756085, 0.79343543644949655],
[0.0006, 0.91447521526646502, 0.91344269916647292, 0.90887608383472995, 0.75920301470001739],
[0.0007, 0.89948104693140796, 0.89806262671772918, 0.89588074035584386, 0.72368378248914489],
[0.0008, 0.88513988471812177, 0.88580406134221268, 0.88239446562191159, 0.69185065009467228],
[0.0009, 0.87323046110428326, 0.87487648784640404, 0.86877953850430911, 0.66372046053257161],
[0.001, 0.85885607498638883, 0.85980496140474927, 0.85664844860965961, 0.63259094557361739],
[0.0011, 0.8476092779392489, 0.84450501093035801, 0.84044913601828819, 0.60160213350598257],
[0.0012, 0.83578054157480974, 0.83627913429522749, 0.82947626868395297, 0.579758977230029],
[0.0013, 0.82304261378902677, 0.82198250415928731, 0.81613998433011226, 0.55214043215600472],
[0.0014, 0.81039305741682821, 0.80982408695539121, 0.8027486112449026, 0.52682450133061276],
[0.0015, 0.79681279903555147, 0.79774447433930951, 0.78800964523805772, 0.50090070094517813],
[0.0016, 0.78701072845735565, 0.78646171168414736, 0.77807690512130223, 0.48159366070895226],
[0.0017, 0.77416292921382979, 0.77397926502336833, 0.76630701324178518, 0.45916047615348965],
[0.0018, 0.76149296583405113, 0.76492778367403447, 0.75406819792782054, 0.43923501790068009],
[0.0019, 0.75063183556619173, 0.74985198011506449, 0.7449011516171804, 0.41927712426196262],
[0.002, 0.74026722329126038, 0.73920642674917225, 0.73430065205897899, 0.40181687200307242],
[0.0021, 0.72841709624248019, 0.73005734729310257, 0.72065839350830918, 0.3832362267809124],
[0.0022, 0.71734912589010902, 0.71962254921941871, 0.7080347786683282, 0.36550214297584327],
[0.0023, 0.70765046307785462, 0.70658433358773332, 0.70138031646279875, 0.35070049017149929],
[0.0024, 0.69418334464854325, 0.69508727248355562, 0.68873369422978914, 0.33232640993103574],
[0.0025, 0.68776398847541698, 0.68684178670863094, 0.67785580651283861, 0.32020894680073053],
]

#Overlap: No rests
dataOverlap_noRests = [
['p', r'$\Pr[X_1]$', r'$\Pr[X_2]$', r'$\Pr[Z \vert X_1, X_2]$', r'$\Pr[X_1, X_2, Z]$'],					
[0.0005, 0.94710964896424565, 0.94591276718659734, 0.95427131842125357, 0.85491555546310449],
[0.0006, 0.93498353341123974, 0.93692446904774573, 0.94686209900390106, 0.82945967372051577],
[0.0007, 0.92628561501042395, 0.92630170781285293, 0.93778719732918203, 0.80464012144966379],
[0.0008, 0.91777034407427638, 0.91586333753448923, 0.9298173838658087, 0.78156005728980849],
[0.0009, 0.90546923243820621, 0.90602872411489643, 0.92160802167622069, 0.7560698333652357],
[0.001, 0.8948339182241084, 0.89568186645318348, 0.91204261063076864, 0.73098985265092919],
[0.0011, 0.88698399461141231, 0.88420463629096724, 0.90404469596976877, 0.70901997970536557],
[0.0012, 0.87735760090531878, 0.87802000755001886, 0.895720248651941, 0.69000692156263266],
[0.0013, 0.8645005979944187, 0.86476579036166756, 0.88682357531792622, 0.66298091812204263],
[0.0014, 0.85677275534334474, 0.85747019008999503, 0.8790126929432861, 0.64577291356508792],
[0.0015, 0.84970257962114137, 0.84653809207123332, 0.87288978893524904, 0.62787451387064319],
[0.0016, 0.83837624506671682, 0.84070976065873215, 0.86217302088183057, 0.60768635208732613],
[0.0017, 0.83015951790145337, 0.82849137504068382, 0.85401472321382821, 0.58737424674986438],
[0.0018, 0.81783182765809592, 0.82304808795143647, 0.84971874309603523, 0.57195836549787116],
[0.0019, 0.81165154512586124, 0.81120883616281447, 0.84047739115817788, 0.55338620380845516],
[0.002, 0.79880782894474234, 0.80296383479563449, 0.83283363315344128, 0.53419098340516158],
[0.0021, 0.7940468517763426, 0.7969715372023024, 0.82823965942785205, 0.52413717311106744],
[0.0022, 0.78687532978983543, 0.78389917431663159, 0.81778184851409041, 0.5044331310514869],
[0.0023, 0.77759887289926533, 0.77741306671697163, 0.80885207713213403, 0.48896363761508227],
[0.0024, 0.76707414460347689, 0.76802716067606469, 0.80082003972067395, 0.47179013494444666],
[0.0025, 0.75955077982875296, 0.76067628625564787, 0.79836494858529727, 0.46127312577658319],
]

#Steane Reichardt
dataReich = [
['p', r'$\Pr[X_1]$', r'$\Pr[X_2]$', r'$\Pr[Z_1 \vert X_1, X_2]$', r'$\Pr[Z_2]$', r'$\Pr[X_3 \vert X_1, X_2, Z_1, Z_2]$', r'$\Pr[X_1, X_2, Z_1, Z_2, X_3]$'],
[0.0005, 0.92865323721619619, 0.92840304438607124, 0.94491802089378996, 0.92858161337468359, 0.95971131883529437, 0.72601394328807023],
[0.0006, 0.91490055428757744, 0.91473651611220685, 0.9347113328581611, 0.9142339264862227, 0.95317980783895073, 0.68167847801238246],
[0.0007, 0.90100034494653325, 0.90077436749537576, 0.92048934985382147, 0.90095384091296205, 0.94526892901030346, 0.63623516431285299],
[0.0008, 0.88884650012970956, 0.88681658318235101, 0.91299122701598368, 0.88582946046341182, 0.94022076383534858, 0.59938674475471787],
[0.0009, 0.87502754153263118, 0.87582516135671962, 0.90198250826744553, 0.87430235774606635, 0.93054418223777269, 0.56238781676251781],
[0.001, 0.8636396046946081, 0.8627537573901275, 0.8922078565413385, 0.86360221842185181, 0.92536042788666184, 0.53126367870306623],
[0.0011, 0.84820545234692757, 0.84960636423695224, 0.8839119376630109, 0.85221397891448658, 0.91637190037204697, 0.49744862329438527],
[0.0012, 0.83726912316525015, 0.83889309579796212, 0.87259683907135843, 0.83750303988326846, 0.90742454764886304, 0.46578151252859312],
[0.0013, 0.82241282339707533, 0.82619359478131338, 0.8620743250594507, 0.82220311596673534, 0.9014693951140359, 0.43415670570927123],
[0.0014, 0.81151443942860679, 0.81311341330425302, 0.85196982397317689, 0.81265900071232322, 0.8944223820256878, 0.40862280249930161],
[0.0015, 0.80245335754043146, 0.79891310810650118, 0.84016789961746274, 0.80167308156627703, 0.88581805297191962, 0.38249631714449678],
[0.0016, 0.79079216975562316, 0.79128682134624517, 0.83096426636998433, 0.78845459343834212, 0.88083996899443306, 0.3611206655346953],
[0.0017, 0.77792111639594363, 0.7758063253045745, 0.82427502271186925, 0.7776371996843795, 0.87472227567747241, 0.33838276177155951],
[0.0018, 0.7636989256590585, 0.7651349774137074, 0.8167822488382932, 0.76499457628912348, 0.86461809818603119, 0.31568155275196697],
[0.0019, 0.75655886456377386, 0.75329531449102793, 0.8074789939269571, 0.75787980531480181, 0.85856070882772118, 0.29944051982660713],
[0.002, 0.74313745322140179, 0.7453462775292411, 0.79762909328469933, 0.74337865949566184, 0.85316952478457464, 0.28020355853634504],
[0.0021, 0.7291141212788127, 0.73316947542621014, 0.78950742600705204, 0.73354512061947785, 0.84585194207605896, 0.26186489852176892],
[0.0022, 0.72009293697711241, 0.72109954029458678, 0.77800473576019358, 0.72423064346183197, 0.83613438351811908, 0.2446352242702198],
[0.0023, 0.71101271221043649, 0.71100626077488427, 0.76799387442572742, 0.71308386852145889, 0.83084081090063144, 0.23002073105478302],
[0.0024, 0.7005515947138109, 0.70191809227579061, 0.75929449465488319, 0.70332664680031998, 0.82430716982376318, 0.2164626459747658],
[0.0025, 0.68835601934593138, 0.68994558065693745, 0.75104592213417332, 0.69303175846760001, 0.81918867553574937, 0.20250302767621262],
]
#Steane Reichardt: No rests
dataReich_noRests = [
['p', r'$\Pr[X_1]$', r'$\Pr[X_2]$', r'$\Pr[Z_1 \vert X_1, X_2]$', r'$\Pr[Z_2]$', r'$\Pr[X_3 \vert X_1, X_2, Z_1, Z_2]$', r'$\Pr[X_1, X_2, Z_1, Z_2, X_3]$'],
[0.0005, 0.93171030470065841, 0.93080766877421983, 0.94805076234821506, 0.93150095087731888, 0.963020030816641, 0.73754931909958177],
[0.0006, 0.91890290079352011, 0.91864579733043661, 0.93754479002436575, 0.9199585113302744, 0.95547487101089246, 0.69566035045925501],
[0.0007, 0.90534638314151816, 0.90533195186100268, 0.92749489400662022, 0.90716216914360503, 0.94916283837655191, 0.65457558679921668],
[0.0008, 0.8914233015677836, 0.8933456561922366, 0.91658045382440168, 0.8951569446315506, 0.94059219684713491, 0.61457467575846159],
[0.0009, 0.87922576041387923, 0.88066591220441959, 0.90781508910757158, 0.8810449794056352, 0.93127211771279572, 0.57674477483527187],
[0.001, 0.8667688481448278, 0.86748382801985502, 0.8976878419858535, 0.86695451835860093, 0.92917804909776813, 0.54373248525465512],
[0.0011, 0.85573664902405555, 0.85613224066680671, 0.88929068150208623, 0.85981648473342831, 0.91998012842922594, 0.51535790251553326],
[0.0012, 0.84252629299259596, 0.84313114620865282, 0.8799100329343722, 0.84556836953668968, 0.91290852656563815, 0.48249504500957702],
[0.0013, 0.82980895053650872, 0.83062414041522037, 0.87047024316397015, 0.83465638466772507, 0.90579710144927539, 0.45360229132210467],
[0.0014, 0.81976456873503212, 0.81896551724137934, 0.86094752299250332, 0.82090168162591559, 0.89768218459936444, 0.42593682782161846],
[0.0015, 0.80979439897446015, 0.80757458420916772, 0.85177405360975389, 0.8095100322594645, 0.89351132078843443, 0.40290635028293992],
[0.0016, 0.79591617922111957, 0.79654247391952315, 0.8435713216584344, 0.80094079527044315, 0.88716975106016782, 0.38001891955527634],
[0.0017, 0.78551464752769051, 0.78414375107641077, 0.83294531078409839, 0.78784017727304201, 0.87895088422458956, 0.35527871815763634],
[0.0018, 0.77424888878986708, 0.77442142912855028, 0.82355903427239507, 0.77616413093591519, 0.87353028529499122, 0.33479905506350371],
[0.0019, 0.76494538874167906, 0.76450071050251911, 0.81492903007772899, 0.76467634739637702, 0.86396074162390057, 0.31484715896992682],
[0.002, 0.75503939317743696, 0.75108128107054639, 0.80624973982544024, 0.75471502052267891, 0.8605259534627564, 0.29694300795058959],
[0.0021, 0.74168408088272408, 0.74249360781816898, 0.79948005281138979, 0.74488294823344403, 0.85125219197439428, 0.27916796775396197],
[0.0022, 0.73170922151948148, 0.7308959734815248, 0.79125760324405081, 0.73479171053772496, 0.84290025118427481, 0.2620911916235385],
[0.0023, 0.72129234995813218, 0.72174052211897832, 0.77974016487529996, 0.72360823619130621, 0.83642811736759348, 0.24568262637872282],
[0.0024, 0.71194040777480871, 0.71112045483629183, 0.77373454330445512, 0.71517678927858563, 0.83063377356923329, 0.23270290076510244],
[0.0025, 0.70265315022651009, 0.70091267445576899, 0.76715052946243101, 0.70516253725782418, 0.82555931643688596, 0.2199495180164108],
]


















#Steane 12
dataSteane12 = [
['p', 'prepA_X1', 'prepA_X3', 'prepA_X2', 'prepA_X4', 'prepA_Z3', 'prepA_Z2', 'prepA_Z1', 'prepA_Z4', 'total'],
[0.0005, 0.92845658335480541, 0.95981493033894782, 0.95648344816867847, 0.9495233392836796, 0.94403638207724649, 0.95736596185662604, 0.93018899067425931, 0.95069418115510296, 0.64686097298192258],
[0.0006, 0.91471797899174012, 0.95459512162321181, 0.94714369110165952, 0.93768167582469109, 0.93289527334316091, 0.94887536479464729, 0.91558302796223157, 0.93901665903567777, 0.59018860565813047],
[0.0007, 0.90124823275541333, 0.94582119357056338, 0.93899704247781857, 0.92976551313758671, 0.92390285213078061, 0.94022309252395275, 0.9031701432367224, 0.93065554478748447, 0.54338405200909434],
[0.0008, 0.88835433168536138, 0.93933157852755844, 0.93094071146245061, 0.92121748102291989, 0.91324300699300698, 0.93096173307490437, 0.8880944865706043, 0.92177575489962982, 0.49807183121416698],
[0.0009, 0.87409161324449935, 0.93170029264667176, 0.92294005767224985, 0.91069704752017189, 0.90319558072862038, 0.92346895867323764, 0.87726147535186361, 0.91243435484943158, 0.45699916018827902],
[0.001, 0.86279210064762546, 0.92511378364529639, 0.91384803191806108, 0.90216881382844361, 0.89195074739219271, 0.9130477759472817, 0.86276933106075526, 0.90285900464282809, 0.41745735054434235],
[0.0011, 0.84933167630101736, 0.91823843939126859, 0.90611274587965851, 0.89432639336052089, 0.88129976050229786, 0.90556869351128966, 0.85147384853533437, 0.89438489841625335, 0.38410671706502608],
[0.0012, 0.83695187453967346, 0.90853468101416768, 0.89844594499740293, 0.88287747426412166, 0.87116002968092998, 0.9007523102126509, 0.83739525885353194, 0.88490445163205678, 0.35072327590382729],
[0.0013, 0.82482708135215199, 0.90217736479489696, 0.89013309357418224, 0.87499781250546871, 0.86367593221955907, 0.89066055675052214, 0.82537179045581721, 0.8745217471151786, 0.32181036332647039],
[0.0014, 0.8130379627737061, 0.89279112754158962, 0.88007182750835122, 0.8654861435668415, 0.85391741965000567, 0.88336213091943305, 0.81306528579224191, 0.86672967863894135, 0.29390208398854084],
[0.0015, 0.80136882223607375, 0.89015691868758917, 0.87522036210157894, 0.85613506386767579, 0.84569069517769591, 0.87784274526898043, 0.80317137446830034, 0.85865090566925428, 0.27366032696430298],
[0.0016, 0.78877879075879709, 0.88264897632391626, 0.86758772313342414, 0.84765876648696303, 0.83333686350927727, 0.86707139602228467, 0.78919453917571247, 0.8492937670079046, 0.24796772838054906],
[0.0017, 0.7778261682816997, 0.8765639889608059, 0.85921028689270329, 0.83934866543562192, 0.82376389030482211, 0.85859241002579956, 0.77896265560165978, 0.84103968713380117, 0.2278405237524754],
[0.0018, 0.76569703549723944, 0.86661864457650517, 0.85182082084444555, 0.83086842367642655, 0.81497172377355542, 0.85107767155060243, 0.76899992010546081, 0.83366350349795659, 0.20883068875076657],
[0.0019, 0.75451397816714882, 0.85923726454486404, 0.84350519220258702, 0.82224670690193891, 0.80415621239961577, 0.84386622259228417, 0.7557573760785381, 0.8208558315334773, 0.18929258598816506],
[0.002, 0.74395707885048945, 0.85269496254956678, 0.83447208402432282, 0.81079328014529417, 0.79609493744884841, 0.83527021535960988, 0.74523061951452385, 0.81703277776305683, 0.17377430449702741],
[0.0021, 0.73288353283069485, 0.84570224626628188, 0.82816277845237962, 0.804052424218059, 0.78726437099365898, 0.82975288215200682, 0.73355418626736457, 0.80688482898219749, 0.15957528816816152],
[0.0022, 0.72162825521806839, 0.84036679680435733, 0.82160166406656265, 0.79260656595279233, 0.7779515699223728, 0.82182126107347575, 0.72378671639893188, 0.79854931199918988, 0.14592969429501695],
[0.0023, 0.71158513427609915, 0.82901254356121323, 0.81196762987045867, 0.78860621737141778, 0.76858821004776701, 0.81470773421739073, 0.71234811286484545, 0.7908766590160663, 0.13325498816974274],
[0.0024, 0.70184246783270676, 0.82781470373672694, 0.80755545012454044, 0.7829872529675217, 0.76069798988777904, 0.80644061375260467, 0.70371278805261595, 0.78159653374458393, 0.12395457249984199],
[0.0025, 0.69014251824884221, 0.81873906285133302, 0.79789636150570209, 0.77108136450558262, 0.75031541576198579, 0.79894531923806711, 0.69259984973930899, 0.77427520657209725, 0.11175569389601699],
]
#Steane 12: No rests
dataSteane12_noRests = [
['p', 'prepA_X1', 'prepA_X3', 'prepA_X2', 'prepA_X4', 'prepA_Z3', 'prepA_Z2', 'prepA_Z1', 'prepA_Z4', 'total'],
[0.0005, 0.92788362300293115, 0.96144766533587922, 0.95490043961727433, 0.94975781175800167, 0.94594594594594594, 0.95565277374382807, 0.92873271471659558, 0.95045947751358573, 0.64562586509452102],
[0.0006, 0.91463043947208444, 0.95238255825717466, 0.94843248870753782, 0.94142456364971472, 0.9339086342377525, 0.94844458730668957, 0.91623094893759038, 0.94031726921851211, 0.59353284206829438],
[0.0007, 0.90071962551526585, 0.94475192402161456, 0.93967426710097723, 0.93224447179028225, 0.92544324897711772, 0.93819861108681579, 0.90134778697647466, 0.92959650582362729, 0.54230800848884597],
[0.0008, 0.88867508767573944, 0.93975999872169758, 0.93099791046666247, 0.9211835366078337, 0.91184998470123702, 0.93137944643684467, 0.88838252048954391, 0.92289119752435689, 0.49871839622785075],
[0.0009, 0.87537475750984661, 0.93093854334116488, 0.92283647952929637, 0.90962014262843838, 0.90351232455834085, 0.92375430636081002, 0.8764139293652754, 0.91266520555223485, 0.4566791410265536],
[0.001, 0.86217350839421447, 0.92322274881516586, 0.91336965258551395, 0.90247820515134558, 0.89416402087929825, 0.91392421768034182, 0.86193893191899185, 0.90288778071118936, 0.41727476494456689],
[0.0011, 0.8482686586299345, 0.91554157381919865, 0.90600138360428917, 0.89161525018723919, 0.88266827788347813, 0.90480493078189039, 0.85113494214531427, 0.89208106646304608, 0.38042851307215825],
[0.0012, 0.83702281982438542, 0.91191157014211943, 0.89625424700437639, 0.88329859025544999, 0.87126761610828096, 0.89779540047581285, 0.83848228939375835, 0.88307514703358758, 0.34998488824266427],
[0.0013, 0.82375913760234798, 0.902101294597067, 0.8896589433394847, 0.87564140733086981, 0.86352755302341166, 0.89039451114922818, 0.82622561204816358, 0.87735660617980116, 0.32265534792471751],
[0.0014, 0.81228699024562223, 0.89528711163805874, 0.88372587873871233, 0.86610081413476525, 0.85494442204688126, 0.88293772176679308, 0.81409086403050956, 0.86877351392024083, 0.29717020084422602],
[0.0015, 0.80061963966808114, 0.88923076923076927, 0.87455090976988548, 0.85636967766245331, 0.84384805763026471, 0.87683782119632958, 0.80312120803559395, 0.85969226238680707, 0.27239224569914405],
[0.0016, 0.78944181679375913, 0.88196318396614848, 0.86593526880115435, 0.84735709322622743, 0.83307475540638953, 0.86681944383235643, 0.78903977242230694, 0.84760687198345208, 0.24673369600400558],
[0.0017, 0.77822957013587302, 0.87549055905220285, 0.85927639627417973, 0.84033613445378152, 0.82718280096775176, 0.8585982481709693, 0.77804806094827983, 0.83872513779055835, 0.22801462024058669],
[0.0018, 0.76647104768740371, 0.86684535490355619, 0.84958367538407409, 0.82808877111626367, 0.81454984941373698, 0.85098586388172448, 0.76708282657797411, 0.83347137099000612, 0.20715403442905386],
[0.0019, 0.75450045286787049, 0.85893048377333892, 0.84380431799469402, 0.82251723173600488, 0.80561156071532392, 0.84453799024715537, 0.75659136868536814, 0.82382197888574182, 0.19074093484740087],
[0.002, 0.74233709432199024, 0.85180676720542814, 0.8371809641970932, 0.8131271242946122, 0.79677060133630284, 0.83835739703055334, 0.74647991119749801, 0.81365284357054013, 0.17463842873853169],
[0.0021, 0.73256578873007006, 0.84668855204018678, 0.82836480427659942, 0.8052437472823023, 0.78500147010498278, 0.82900094791791834, 0.73452386758114352, 0.80788196567740933, 0.15977125375618759],
[0.0022, 0.72269279624133032, 0.84000170847081179, 0.82031950614194249, 0.7951653944020356, 0.77928853754940708, 0.82042717534543275, 0.72380215660689318, 0.79931864695488575, 0.14647130397220137],
[0.0023, 0.7119012973922092, 0.83423792244311423, 0.81512571088012764, 0.78578051578633057, 0.76981663642441023, 0.81166130925047197, 0.71366570492444614, 0.78853708408203727, 0.13375642317817243],
[0.0024, 0.70079877189547934, 0.82367400392887724, 0.8052354783430834, 0.78121338062278334, 0.76352993661928492, 0.80686560014119491, 0.70286991703531121, 0.7827964090897972, 0.12308136856493578],
[0.0025, 0.69059026359134268, 0.82070379965457685, 0.79794105580521124, 0.77165257114636709, 0.74896919718651467, 0.80046449572565104, 0.69302022376519812, 0.77476594167374124, 0.11233678070729557],
]

def plot(data, name):
	# First row is the labels
	# First column is the X values
	ylabels = data.pop(0)
	xlabel = ylabels.pop(0)

	X = []
	yList = [[] for _ in range(len(data[0])-1)]
	for r in range(len(data)):
		row = data[r]
		X.append(row.pop(0))
		for j, col in enumerate(row):
			yList[j].append(col)
			
	print 'X=',X
	print 'yList=', yList
	
	plotList(X, yList, name + 'Plot', labelList=ylabels, xLabel=xlabel)
	
	
def getyvals(data, index):	
	return [row[index] for row in data[1:]]

def getxvals(data):
	return [row[0] for row in data[1:]]

def qubitOverhead(qubitTime, prAccept):
	return qubitTime / prAccept

def overhead4(data, measX, measZ):
	
	X = sorted(data.keys())
	
	overheadList = []
	for p in X:
		x1 = data[p]['prepA0']
		x2 = data[p]['prepA2']
		z = data[p]['prepA']
		
		overheadSample = [measX * (x1[s] + x2[s]) + measZ * z[s] for s in xrange(len(z))]
		mean, sigma = getStats(overheadSample)		
		overheadList.append((mean,sigma))
		
	return X, overheadList


def overhead6(data, measX, measZ):
	
	prAcceptList = [getyvals(data, i) for i in range(1,6)]
	X = getxvals(data)
	
	overheadList = []
	for p in range(len(X)):
		x1 = measX / prAcceptList[0][p]
		x2 = measX / prAcceptList[1][p]
		z1 = (x1 + x2 + measZ) / prAcceptList[2][p]
		z2 = measX / prAcceptList[3][p]
		x3 = (z1 + z2 + measZ) / prAcceptList[4][p]
		
		overheadList.append(x3)
		
	return X, overheadList


def overhead12(data, meas1, meas2, meas3, meas4):
	
	ylabels = data[0][1:]
	prAcceptList = [getyvals(data, i) for i in range(1,len(ylabels)+1)]
	prAcceptList = dict(zip(ylabels, prAcceptList))
	X = getxvals(data)
	
	overheadList = []
	for p in range(len(X)):
		x1 = meas1 / prAcceptList['prepA_X1'][p]
		z1 = meas1 / prAcceptList['prepA_Z1'][p]
		x2 = (x1 + meas2) / prAcceptList['prepA_X2'][p]
		z2 = (z1 + meas2) / prAcceptList['prepA_Z2'][p]		
		z3 = (2*x1 + meas3) / prAcceptList['prepA_Z3'][p]
		x3 = (z3 + z1 + meas3) / prAcceptList['prepA_X3'][p]		
		z4 = (x3 + x2 + meas4) / prAcceptList['prepA_Z4'][p]
		x4 = (z4 + z2 + meas4) / prAcceptList['prepA_X4'][p]
		
		overheadList.append(x4)
		
	return X, overheadList


if __name__ == '__main__':
#	plot(dataSteaneRandom, 'prAcceptRandomSteane')
#	plot(dataSteaneRandom_noRests, 'prAcceptRandomSteaneNoRests')
#	plot(dataOverlap, 'prAcceptOverlap')
#	plot(dataOverlap_noRests, 'prAcceptOverlapNoRests')
#	plot(dataReich, 'prAcceptReich')
#	plot(dataReich_noRests, 'prAcceptReichNoRests')
#	plot(dataSteane12, 'prAcceptSteane12')
#	plot(dataSteane12_noRests, 'prAcceptSteane12_noRests')
	
	pMin = 5e-4
	pMax = 2e-3
	pStep = 1e-4
	iters = 50	
	
	dataList = [ ('Random-4', dataSteaneRandom),
				 ('RandomSteaneNR', dataSteaneRandom_noRests),
				 ('Overlap-4', simOverlapPrep(pMin, pMax, pStep, iters)),
				 ('OverlapNR', dataOverlap_noRests),
				 ('Opt-6', dataReich),
				 ('ReichardtNR', dataReich_noRests),
				 ('Steane-12', dataSteane12),
				 ('Steane12NR', dataSteane12_noRests)
				]
	
#	ylist = []
#	labels = []
#	X = [0.0005 + 0.0001*i for i in range(21)]
#	for name, data in dataList:
#		if name.endswith('NR'):
#			continue
#		ylist.append(getyvals(data, -1))
#		labels.append(name)
#		
#	print X[5]
#	print [y[5] for y in ylist]
	#plotList(X, ylist, 'plotComparePrAccept', labelList=labels, xLabel='p', yLabel='Pr[accept]')
	
	qubitTimeX = 46*9 + 23
	qubitTimeZ = 46*2
	qubitTime1 = qubitTimeX
	qubitTime2 = 23*12
	qubitTime3 = qubitTimeZ
	qubitTime4 = qubitTime3
	
	X4, overheadOverlap = overhead4(simOverlapPrep(pMin, pMax, pStep, iters), qubitTimeX, qubitTimeZ)
	print X4
	print overheadOverlap
	
	raise Exception
	
	X4, overheadRandom = overhead4(dataSteaneRandom, qubitTimeX, qubitTimeZ)

	X6, overheadReic = overhead6(dataReich, qubitTimeX, qubitTimeZ)
	X12, overheadSteane12 = overhead12(dataSteane12, qubitTime1, qubitTime2, qubitTime3, qubitTime4)
	
	yList = [overheadRandom, overheadOverlap, overheadReic, overheadSteane12]
	labels = ['Random-4', 'Overlap-4', 'Opt-6', 'Steane-12']
	print X[5]
	print [y[5] for y in yList]
	plotList(X4, yList, 'plotQubitOverheadCompare', labelList=labels, xLabel='p', yLabel='Qubits', legendLoc='upper left')
	
	cnotsX = 77*2 + 23
	cnotsZ = 23
	cnots1 = cnotsX
	cnots2 = 77 + 23
	cnots3 = 23
	cnots4 = cnots3

	X4, overheadRandom = overhead4(dataSteaneRandom, cnotsX, cnotsZ)	
	X6, overheadReic = overhead6(dataReich, cnotsX, cnotsZ)
	X12, overheadSteane12 = overhead12(dataSteane12, cnots1, cnots2, cnots3, cnots4)
	
	cnotsX = 57*2 + 23
	X4, overheadOverlap = overhead4(dataOverlap, cnotsX, cnotsZ)
	
	yList = [overheadRandom, overheadOverlap, overheadReic, overheadSteane12]
	labels = ['Random-4', 'Overlap-4', 'Opt-6', 'Steane-12']
	print X[5]
	print [y[5] for y in yList]
	plotList(X4, yList, 'plotCnotOverheadCompare', labelList=labels, xLabel='p', yLabel='CNOTs', legendLoc='upper left')
	
	cnotsX = 77*2 + 23
	X4, overheadRandom = overhead4(dataSteaneRandom_noRests, cnotsX, cnotsZ)	
	X6, overheadReic = overhead6(dataReich_noRests, cnotsX, cnotsZ)
	X12, overheadSteane12 = overhead12(dataSteane12_noRests, cnots1, cnots2, cnots3, cnots4)
	
	cnotsX = 57*2 + 23
	X4, overheadOverlap = overhead4(dataOverlap_noRests, cnotsX, cnotsZ)
	
	yList = [overheadRandom, overheadOverlap, overheadReic, overheadSteane12]
	labels = ['Random-4', 'Overlap-4', 'Opt-6', 'Steane-12']
	print X[5]
	print [y[5] for y in yList]
	#plotList(X4, yList, 'plotCnotOverheadNoRestsCompare', labelList=labels, xLabel='p', yLabel='CNOTs', legendLoc='upper left')
